package com.choosefine.it.basedata.service;

import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.basedata.dao.PriceAdjustLogMapper;
import com.choosefine.it.basedata.dao.ProjectInfoMapper;
import com.choosefine.it.basedata.model.ProjectInfo;
import com.choosefine.it.basedata.model.dto.ProjectInfoSaveDTO;
import com.choosefine.it.basedata.model.dto.ProjectInfoUpdateDTO;
import com.choosefine.it.basedata.model.vo.ProjectInfoNameVO;
import com.choosefine.it.basedata.model.vo.ProjectInfoVO;
import com.choosefine.it.basedata.model.vo.ProjectStartEndTimeVO;
import com.choosefine.it.basedata.model.vo.projectInfoAppVO;
import com.choosefine.it.common.utils.DateUtils;
import com.choosefine.it.common.utils.IDCreatUtil;
import com.choosefine.it.team.dao.AuthorizationPictureMapper;
import com.choosefine.it.team.model.AuthorizationPicture;
import com.choosefine.it.team.model.PriceAdjustLog;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 项目详情service
 * @author guwenwei
 *
 */
@Service("projectinfoService")
public class ProjectInfoService{
	@Resource
	ProjectInfoMapper projectmapper;
	@Resource
	PriceAdjustLogMapper priceAdjustLogMapper;
	
	@Autowired
	AuthorizationPictureMapper authorizationPictureMapper;
	
	public int deleteByPrimaryKey(Integer id) {
		
		return projectmapper.deleteByPrimaryKey(id);
	}

	public ProjectInfo findByProjectCode(String projectCode) {
		
		return projectmapper.findByProjectCode(projectCode);
	}

	public int deleteByProjectCode(Map<String,Object> UserCode) {
		
		return projectmapper.deleteByProjectCode(UserCode);
	}

	public Page<ProjectInfo> selectAll(PageDTO pageDto) {
		PageHelper.startPage(pageDto.getPageNum(), pageDto.getPageSize());
		Page<ProjectInfo> list=projectmapper.selectAll();
		return list;
	}

	public int insert(ProjectInfo record) {
		
		return projectmapper.insert(record);
	}

	public int update(ProjectInfo record) {
		
		return projectmapper.updateByPrimaryKey(record);
	}

	public ProjectInfo findByPrimaryKey(Integer id) {
		
		return projectmapper.selectByPrimaryKey(id);
	}

	public int countProject(){
		return projectmapper.countProject();
	}

	
	public Page<ProjectInfo> selectAllByCondition(Map<String, Object>params,PageDTO pageDto){
		PageHelper.startPage(pageDto.getPageNum(), pageDto.getPageSize());
		Page<ProjectInfo> list=projectmapper.selectAllByCondition(params);
		return list;
	}
//	/**
//	 * 查询项目基本信息（控制台-项目管理-项目资金统计)
//	 * @author chenjian
//	 */
//	public Page<ProjectInfo> findAllByCondition(Map<String, Object>params,PageDTO pageDto){
//		PageHelper.startPage(pageDto.getPageNum(), pageDto.getPageSize());
//		Page<ProjectInfo> list=projectmapper.findAllByCondition(params);
//		return list;
//	}
	
	public int updateProjectInfoProjectDataStatus(Map<String, Object>params){
		return projectmapper.updateProjectInfoProjectDataStatus(params);
		
	}
	/**
	 * 查询项目基本信息（控制台-项目管理-项目基本资料 )
	 * @author chenjian
	 */
	public Page<ProjectInfo> findProjectAllByCondition(Map<String, Object>params,PageDTO pageDto){
		PageHelper.startPage(pageDto.getPageNum(), pageDto.getPageSize());
		Page<ProjectInfo> list=projectmapper.findProjectAllByCondition(params);
		return list;
	}
	public int batchUpdateProjectCourse(Map<String, Object>params){
		return projectmapper.batchUpdateProjectCourse(params);
	}
	public int batchDeleteProjects(Map<String, Object>params){
		return projectmapper.batchDeleteProjects(params);
	}
	
	public int saveProjectInfo(ProjectInfo record) {
		return projectmapper.saveProjectInfo(record);
	}
	/**
	 * 用户查询自己的项目
	 * @author chenjian
	 */
	public Page<ProjectInfo> findProjectInfoByManager(String userCode) {
		return projectmapper.findProjectInfoByManager(userCode);
	}

//	/**
//	 * 根据项目编码获取班组资金结算总额
//	 * @param projectCode
//	 * @author yeliang
//	 * @return
//	 */
//	public Double getTeamProjectCapitals(String projectCode) {
//
//		return projectmapper.getTeamProjectCapital(projectCode);
//	}
	
	public ProjectInfoVO queryByProjectCode(String projectCode) {
		return projectmapper.queryByProjectCode(projectCode);
	}
	
	/**
	 * 保存项目信息
	 * @author chenjian
	 * @time 2016-12-19 下午7:06:39
	 */
	@Transactional
	public String saveProjectInfo(ProjectInfoSaveDTO projectInfoSaveDTO) {
		ProjectInfo projectInfo = new ProjectInfo();
		String projectCode=IDCreatUtil.getCode("PC", 5);
		projectInfo.setOperatorCode(projectInfoSaveDTO.getOperatorCode());
		//直接输入建设单位名称，暂不需要code
//		projectInfo.setConstructUnitCode(projectInfoSaveDTO.getConstructUnitCode());
		projectInfo.setManagerUserCode(projectInfoSaveDTO.getManagerUserCode());
		projectInfo.setConstructUserCode(projectInfoSaveDTO.getConstructUserCode());
		projectInfo.setProjectName(projectInfoSaveDTO.getProjectName());
		projectInfo.setContractorName(projectInfoSaveDTO.getContractorName());
		projectInfo.setConstructionUnit(projectInfoSaveDTO.getConstructionUnit());
		projectInfo.setProjectType(projectInfoSaveDTO.getProjectType());
		projectInfo.setProjectAddress(projectInfoSaveDTO.getProjectAddress());
		projectInfo.setProjectStage(projectInfoSaveDTO.getProjectStage());
		projectInfo.setProjectStart(projectInfoSaveDTO.getProjectStart());
		projectInfo.setProjectEnd(projectInfoSaveDTO.getProjectEnd());
//		projectInfo.setProjectArea(projectInfoSaveDTO.getProjectArea());
		projectInfo.setProjectScale(projectInfoSaveDTO.getProjectScale());
		projectInfo.setProjectCourse(projectInfoSaveDTO.getProjectCourse());
		projectInfo.setProjectDataStatus(projectInfoSaveDTO.getProjectDataStatus());
		projectInfo.setConstructUnit(projectInfoSaveDTO.getConstructUnit());
		projectInfo.setProjectCode(projectCode);
		projectInfo.setProjectTotalPrice(projectInfoSaveDTO.getProjectTotalPrice());
		projectInfo.setAdjustProjectPrice(projectInfoSaveDTO.getAdjustProjectPrice());
		projectInfo.setAdjustReason(projectInfoSaveDTO.getAdjustReason());
		projectInfo.setEngineeringStandard(projectInfoSaveDTO.getEngineeringStandard());
		projectInfo.setProvinceArea(projectInfoSaveDTO.getProvinceArea());
		projectInfo.setProvinceCode(projectInfoSaveDTO.getProvinceCode());
		projectInfo.setCityArea(projectInfoSaveDTO.getCityArea());
		projectInfo.setCityCode(projectInfoSaveDTO.getCityCode());
		projectInfo.setCtime(DateUtils.getNowTime());
		projectInfo.setIsValid(0);
		//项目信息保存
		projectmapper.saveProjectInfo(projectInfo);
		
		//保存项目承包人授权书信息
		List<AuthorizationPicture> contractorAuthorizationList = authorizationPictureList(projectInfoSaveDTO.getContractorAuthorization(),projectInfo.getId(),1);
		if(contractorAuthorizationList.size()>0){
			authorizationPictureMapper.saveAuthorizationPictureList(contractorAuthorizationList);
		}
		
		//保存承建单位授权书信息
		List<AuthorizationPicture> constructAuthorizationList = authorizationPictureList(projectInfoSaveDTO.getConstructAuthorization(),projectInfo.getId(),2);
		if(constructAuthorizationList.size()>0){
			authorizationPictureMapper.saveAuthorizationPictureList(constructAuthorizationList);
		}
		// TODO 发送推送通知承包人和承包单位 已加入项目组 ,传递项目编号projectId以查看详情 [您已加入"下沙建筑公司--龙湖滟澜山"项目,点击查看详情...]
		return projectCode;
	} 
	
	
	/**
	 * 项目相关授权书集合
	 * @author chenjian
	 * @time 2016年12月30日下午2:44:03
	 */
	private List<AuthorizationPicture> authorizationPictureList(String projectAuthorization,int projectId,int type){
		List<AuthorizationPicture> authorizationPictureList = new ArrayList<>();
		if(projectAuthorization!=null&&!"".equals(projectAuthorization)){
			String[] pictureList = projectAuthorization.split(",");
			for(int i=0;i<pictureList.length;i++){
				AuthorizationPicture authorizationPicture = new AuthorizationPicture();
				authorizationPicture.setOriginalPicture(pictureList[i]);
				authorizationPicture.setType(type);
				authorizationPicture.setCtime(DateUtils.getNowTime());
				authorizationPicture.setIsValid(0);
				authorizationPicture.setItemId(projectId);
				authorizationPicture.setItemType(1);
				authorizationPictureList.add(authorizationPicture);
			}
		}
		return authorizationPictureList;
	}
	
	
	/**
	 * 更新项目信息
	 * @author chenjian
	 * @time 2016-12-21 下午3:54:06
	 */
	@Transactional
	public int updateProjectInfo(ProjectInfoUpdateDTO projectInfoUpdateDTO){
		ProjectInfo projectInfo = new ProjectInfo();
		projectInfo.setManagerUserCode(projectInfoUpdateDTO.getManagerUserCode());
		projectInfo.setConstructUserCode(projectInfoUpdateDTO.getConstructUserCode());
		projectInfo.setProjectName(projectInfoUpdateDTO.getProjectName());
		projectInfo.setContractorName(projectInfoUpdateDTO.getContractorName());
		projectInfo.setConstructionUnit(projectInfoUpdateDTO.getConstructionUnit());
		projectInfo.setProjectType(projectInfoUpdateDTO.getProjectType());
		projectInfo.setProjectAddress(projectInfoUpdateDTO.getProjectAddress());
		projectInfo.setProjectStage(projectInfoUpdateDTO.getProjectStage());
		projectInfo.setProjectStart(projectInfoUpdateDTO.getProjectStart());
		projectInfo.setProjectEnd(projectInfoUpdateDTO.getProjectEnd());
//		projectInfo.setProjectArea(projectInfoUpdateDTO.getProjectArea());
		projectInfo.setProjectScale(projectInfoUpdateDTO.getProjectScale());
		projectInfo.setProjectCourse(projectInfoUpdateDTO.getProjectCourse());
		projectInfo.setProjectDataStatus(projectInfoUpdateDTO.getProjectDataStatus());
		projectInfo.setConstructUnit(projectInfoUpdateDTO.getConstructUnit());
		projectInfo.setProjectTotalPrice(projectInfoUpdateDTO.getProjectTotalPrice());
		projectInfo.setAdjustProjectPrice(projectInfoUpdateDTO.getAdjustProjectPrice());
		projectInfo.setAdjustReason(projectInfoUpdateDTO.getAdjustReason());
		projectInfo.setEngineeringStandard(projectInfoUpdateDTO.getEngineeringStandard());
		projectInfo.setProvinceArea(projectInfoUpdateDTO.getProvinceArea());
		projectInfo.setProvinceCode(projectInfoUpdateDTO.getProvinceCode());
		projectInfo.setCityArea(projectInfoUpdateDTO.getCityArea());
		projectInfo.setCityCode(projectInfoUpdateDTO.getCityCode());
		projectInfo.setUtime(DateUtils.getNowTime());
		projectInfo.setId(projectInfoUpdateDTO.getId());
		//项目信息保存
		int result = projectmapper.updateByPrimaryKeySelective(projectInfo);
		//保存更改项目总价记录
		if(projectInfoUpdateDTO.getAdjustProjectPrice() !=null && projectInfoUpdateDTO.getAdjustProjectPrice().intValue() >0){
			PriceAdjustLog priceAdjustLog = new PriceAdjustLog(projectInfoUpdateDTO.getId(),
					projectInfoUpdateDTO.getAdjustProjectPrice(), projectInfoUpdateDTO.getAdjustReason(), 1,//1表示项目，2表示班组
					projectInfoUpdateDTO.getOperatorCode());
			priceAdjustLogMapper.savePriceAdjustRecord(priceAdjustLog);
			priceAdjustLog = null;
		}
		
		//删除项目承包人授权书信息
		authorizationPictureMapper.deleteByProjectId(projectInfoUpdateDTO.getId());
		
		//保存项目承包人授权书信息
		List<AuthorizationPicture> contractorAuthorizationList = authorizationPictureList(projectInfoUpdateDTO.getContractorAuthorization(),projectInfo.getId(),1);
		if(contractorAuthorizationList.size()>0){
			authorizationPictureMapper.saveAuthorizationPictureList(contractorAuthorizationList);
		}
		
		//保存承建单位授权书信息
		List<AuthorizationPicture> constructAuthorizationList = authorizationPictureList(projectInfoUpdateDTO.getConstructAuthorization(),projectInfo.getId(),2);
		if(constructAuthorizationList.size()>0){
			authorizationPictureMapper.saveAuthorizationPictureList(constructAuthorizationList);
		}
		
		// TODO 如果承包人或者承包单位发生变化,通知旧的用户和新的用户相关信息
		return result;
	}

	/**
	 * @author chenjian
	 * @time 2016年12月22日下午8:40:22
	 */
	public Page<ProjectInfo> findProjectListByUserCode(PageDTO pageDTO,String userCode) {
		PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
		return projectmapper.findProjectListByUserCode(userCode);
	}

	/**
	 * 根据名称查询项目信息      add by juchengyang 2017/2/20
	 */
	public ProjectInfo selectProjectInfoByName(String name){
		return projectmapper.selectProjectInfoByName(name);
	}

	public Page<ProjectInfoNameVO> findProjectNameNoPage(Map<String,Object> params) {
		return projectmapper.findProjectNameNoPage(params);
	}

	public Page<ProjectInfoNameVO> findProjectByName(String projectName) {
		return projectmapper.findProjectByName(projectName);
	}

	public Page<ProjectInfoNameVO> findProjectName(Map<String, Object> map) {
		return projectmapper.findProjectName(map);
	}
	/**
	 * add 发布时，根据项目经理userCode，名字查询项目信息
	 *  juchengyang 2017年3月1日
	 */
	public List<projectInfoAppVO> findProjectByUserCodeAndProjectName(Map<String,Object> params){
		return projectmapper.findProjectByUserCodeAndProjectName(params);
	}

	public List<ProjectStartEndTimeVO> findProjectStartEndTime(String projectCode) {
		return projectmapper.findProjectStartEndTime(projectCode);
	}
}
